<?php
class mobile extends Controller
{
	
	function __constructor()
	{
		parent::__constructor();
	}
	
	
	function index()
	{
		is_logged_in("mobile/login");
		redirect("mobile/home");
	}
	
	function home()
	{
		is_logged_in("mobile/login");
		$this->load->view("mobile/home");
	}
	
	function login()
	{
		
		$_SESSION['user_id']=0;
		$msg = "";
		$u = $this->input->post("username");
		$p = $this->input->post("password");
		$a = $this->input->post("action");
		
		if($u != "" && $p != "" && $a == "login")
		{
			$this->load->model("model_user");
			$r = $this->model_user->isValidUser(array("username" => $u, "password" =>md5($p), "active" => 1));	
			if($r != 0)
			{
				$_SESSION['user_id'] = $r[0]['id'];
				$_SESSION['user_fullname'] = $r[0]['firstname'] ." ". $r[0]['lastname'];
				$_SESSION['user_type'] = $r[0]['user_type'];

				$this->model_user->update_last_login($_SESSION['user_id']);
				
				redirect("mobile/home");
			}
			else{
				$msg = "Invalid User!";
				//redirect("user/login/");
			}
		}
		$data = array("msg" => $msg);
		
		$this->load->view("mobile/login", $data);
	
	}

	function viewjobs()
	{
		is_logged_in("mobile/login");
		
		$msg= "";
		$this->load->model("model_job");
		$allJobs = $this->model_job->getJobs(99999999);
		
		$data = array(	"crum" => build_crum( array("Jobs", "View Jobs") )
							  ,"jobs" => $allJobs
							  ,"msg" => $msg);
		$this->load->view("mobile/viewjobs",$data);
	}

	function newjob()
	{
		is_logged_in("mobile/login");
		
		$this->load->model("model_lu");
		$this->load->model("model_company");
		$msg = "";
		
		$companies = $this->model_company->getAll();
		$job_pri = $this->model_lu->get("job_pri");
		$job_types = $this->model_lu->get("job_type");
		
		
		
		if($this->input->post("action") == "newJob")
		{
			if($this->input->post("job_subject") != "")
			{
				$data = array(	"company_id" => $this->input->post("company_id")
								,"job_subject" => $this->input->post("job_subject")
								,"job_description" => $this->input->post("job_description")
								,"job_logged" => date("Y-m-d h:i:s")
								,"job_logged_by" => $_SESSION['user_id']
								,"job_start" => $this->input->post("job_start_date"). " " . $this->input->post("job_start_time"). ":00"
								,"job_end" => $this->input->post("job_end_date"). " " . $this->input->post("job_end_time"). ":00"
								,"job_type" => $this->input->post("job_type")
								,"job_priority" => $this->input->post("job_pri")
							);
				$this->load->model("model_job");
				$this->model_job->newJob($data);
				$msg = $this->input->post("job_subject") . " : Sucessfully added!";
			}
			else{
				$msg = "* Job subject required!";
			}
		}
		
		$data = array(	"crum" => build_crum( array("Jobs", "New Job") )
					  ,"companies" => $companies
					  ,"job_pri" => $job_pri
					  ,"job_types" => $job_types
					  ,"msg" => $msg);
					  
		$this->load->view("mobile/newjob", $data);
	}
	
	
	function viewclients()
	{

		is_logged_in("mobile/login");
		$this->load->model("model_company");
		$this->load->model("model_lu");
		
		$states = $this->model_lu->get("states");
		$this->load->model("model_company");
		
		$data = array(	"crum" => build_crum( array("Client", "View Clients") )
					  	,"clients" => $this->model_company->getAll()
						);
		
		$this->load->view("mobile/viewclients", $data);	
	}
	
	function newclient()
	{
		is_logged_in("mobile/login");
		$this->load->model("model_company");
		$this->load->model("model_lu");
		
		$msg = "";
		$states = $this->model_lu->get("states");
		$name = $this->input->post("client_name");
		if($name != "")
		{
			$company = array(
							"company_name" => $this->input->post("client_name")
							,"company_contact1" => $this->input->post("client_contact1")
							,"company_contact2" => $this->input->post("client_contact2")
							,"company_phone1" => $this->input->post("client_phone1")
							,"company_phone2" => $this->input->post("client_phone2")
							,"company_address1" => $this->input->post("client_address1")
							,"company_suburb" => $this->input->post("client_suburb")
							,"company_state" => $this->input->post("client_state")
							,"company_postcode" => $this->input->post("client_postcode")
							,"company_email" => $this->input->post("client_email")
			);
			
			$this->load->model("model_company");
			$this->model_company->newCompany($company);
			$msg = $name . " Added";
		}
		$data = array(	"crum" => build_crum( array("Client", "New Client") )
						,"states" => $states
					  ,"msg" => $msg);
		$this->load->view("mobile/newclient",$data);
	}
}